package edu.xlh.travel.util;

import edu.xlh.travel.dao.ForeHotelOrderDao;
import edu.xlh.travel.enumeration.OrderStatusEnum;
import edu.xlh.travel.pojo.Hotel_orders;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DoubleChaineUpdateStatus {

    @Autowired
    private ForeHotelOrderDao foreHotelOrderDao;

    public void syncHotelOrder(Hotel_orders hotel_orders , String status) {
        if (hotel_orders.getHeadOrderId() == null && hotel_orders.getTailOrderId() == null) {
            foreHotelOrderDao.updateStatusByHotelOrderId(status, hotel_orders.getId());
        } else if (hotel_orders.getHeadOrderId() != null && hotel_orders.getTailOrderId() == null) {
            tailUpdate(hotel_orders , status);
        } else {
            headUpdate(hotel_orders , status);
        }
    }
    public void tailUpdate(Hotel_orders hotel_orders , String status) {
        boolean flag = false;
        String tempOrderId = hotel_orders.getHeadOrderId();
        Hotel_orders tempHotel_orders = null;
        while (!flag) {
            if (StringUtils.isNotEmpty(tempOrderId)) {
                tempHotel_orders = foreHotelOrderDao.findById(tempOrderId).get();
                tempOrderId = tempHotel_orders.getTailOrderId();
                foreHotelOrderDao.updateStatusByHotelOrderId(status, tempHotel_orders.getId());
            } else {
                flag = true;
            }
        }
    }

    public void headUpdate(Hotel_orders hotel_orders , String status) {
        boolean flag = false;
        String tempOrderId = hotel_orders.getHeadOrderId();
        Hotel_orders tempHotel_orders = null;
        while (!flag) {
            if (StringUtils.isNotEmpty(tempOrderId)) {
                tempHotel_orders = foreHotelOrderDao.findById(tempOrderId).get();
                tempOrderId = tempHotel_orders.getTailOrderId();
                foreHotelOrderDao.updateStatusByHotelOrderId(status, tempHotel_orders.getId());
            } else {
                flag = true;
            }
        }
    }
}
